package com.neusoft.servlet;

import com.alibaba.fastjson.JSON;
import com.neusoft.dto.ArticleDto;
import com.neusoft.entity.ArticleQueryCondition;
import com.neusoft.entity.ArticlesEntity;
import com.neusoft.mapper.ArticleMapper;
import com.neusoft.utils.SqlSessionFactoryUtils;
import com.neusoft.utils.UserUtils;
import com.neusoft.vo.Article;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet("/api/articles")
public class ArticleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSessionFactory sqlSessionFactory
                = SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        String tagCondition = req.getParameter("tag");
        String authorCondition = req.getParameter("author");
        String StringLimit = req.getParameter("limit");
        String StringOffset = req.getParameter("offset");
        long limit = 20;
        if(StringLimit != null){
            limit = Long.valueOf(StringLimit);
        }
        long offset = 0;
        if(StringOffset != null){
            offset = Long.valueOf(StringOffset) ;
        }
        Long loginUserId = UserUtils.getLoginUserId();
        if(loginUserId == null){
            loginUserId = 0L;
        }
        ArticleQueryCondition articleQueryCondition = new ArticleQueryCondition();
        articleQueryCondition.setUid(loginUserId);
        articleQueryCondition.setAuthor(authorCondition);
        articleQueryCondition.setTag(tagCondition);
        articleQueryCondition.setLimit(limit);
        articleQueryCondition.setOffset(offset);

        List<Article> allArticles = mapper.getAllArticles(articleQueryCondition);
        sqlSession.close();

        String jsonStr = JSON.toJSONString(allArticles);

        PrintWriter printWriter = resp.getWriter();
        printWriter.println(jsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonReq = IOUtils.toString(req.getInputStream(), StandardCharsets.UTF_8);
        ArticleDto articleDto = JSON.parseObject(jsonReq, ArticleDto.class);

        long userid = UserUtils.getLoginUserId();
        com.neusoft.dto.Article article = articleDto.getArticle();

        ArticlesEntity articlesEntity = new ArticlesEntity();

        articlesEntity.setTitle(article.getTitle());
        articlesEntity.setDescription(article.getDescription());
        articlesEntity.setBody(article.getBody());

        articlesEntity.setUserId(userid);
        UUID uuid = UUID.randomUUID();
        articlesEntity.setSlug(uuid.toString());
        articlesEntity.setCreatedAt(new Timestamp(new Date().getTime()));
        articlesEntity.setUpdatedAt(new Timestamp(new Date().getTime()));

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
//        int rows = mapper.createArticle(articlesEntity);
        sqlSession.commit();

        Article articleVo = mapper.getArticleById(userid, articlesEntity.getId());

        sqlSession.close();
        String retJson = JSON.toJSONString(articleVo);
        PrintWriter writer = resp.getWriter();
        writer.println(retJson);
        writer.close();
    }
}
